﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;

using Microsoft.Win32;

namespace iPdf
{
    public enum PdfCreatorFormat
    {
        PDF = 0,
        PDF_A = 9,
        PDF_X = 10,
        TIFF = 5,
        JPEG = 1,
        PNG = 2,
        BMP = 3,
        ICO = 4,
        GIF = 6,
        WMF = 7,
        EMF = 8,
        EXIF = 11
    }

    

    internal class PdfCreatorOption
    {
        private static string autoSaveDirectory;
        private static string autoSaveFileName;
        private static string useAutoSave;
        private static string useAutoSaveDirectory;
        private static string autoSaveFormat;
        private static string autoSaveStartStandardProgram;
        private static string runProgramAfterSaving;
        private static string runProgramAfterSavingProgramme;
        private static string runProgramAfterSavingProgramName;
        private static string runProgramBeforeSaving;
        private static string runProgramBeforeSavingProgramme;
        private static string runProgramBeforeSavingProgramName;

        public const string PdfCreatorProgramRegistry = @"Software\PDFCreator\Program";
        public const string GhostScriptRegistry = @"Software\PDFCreator\GhostScript";
        public const string GhostScriptBinDirRegistry = "DirectoryGhostscriptBinaries";
        public const string GhostScriptLibDirRegistry = "DirectoryGhostscriptLibraries";

        private static Dictionary<PdfCreatorFormat, string> formatExtensionDict = new Dictionary<PdfCreatorFormat, string>();

        static PdfCreatorOption()
        {
            useAutoSave = "1";
            useAutoSaveDirectory = "1";
            autoSaveDirectory = string.Empty;
            autoSaveFileName = string.Empty;
            autoSaveFormat = "0";
            autoSaveStartStandardProgram = "0";
            runProgramAfterSaving = string.Empty;
            runProgramAfterSavingProgramme = string.Empty;
            runProgramAfterSavingProgramName = string.Empty;
            runProgramBeforeSaving = string.Empty;
            runProgramBeforeSavingProgramme = string.Empty;
            runProgramBeforeSavingProgramName = string.Empty;

            formatExtensionDict.Add(PdfCreatorFormat.PDF, Global.PdfExt);
            formatExtensionDict.Add(PdfCreatorFormat.PDF_A, Global.PdfExt);
            formatExtensionDict.Add(PdfCreatorFormat.PDF_X, Global.PdfExt);
            formatExtensionDict.Add(PdfCreatorFormat.JPEG, ".jpg");
            formatExtensionDict.Add(PdfCreatorFormat.PNG, ".png");
            formatExtensionDict.Add(PdfCreatorFormat.BMP, ".bmp");
            formatExtensionDict.Add(PdfCreatorFormat.ICO, ".ico");
            formatExtensionDict.Add(PdfCreatorFormat.GIF, ".gif");
            formatExtensionDict.Add(PdfCreatorFormat.TIFF, ".tif");
            formatExtensionDict.Add(PdfCreatorFormat.WMF, ".wmf");
            formatExtensionDict.Add(PdfCreatorFormat.EMF, ".emf");
            formatExtensionDict.Add(PdfCreatorFormat.EXIF, ".exif");
        }

        private static bool IsValidPdfCreatorFileName(string fileName)
        {
            char dot = '.';
            int dotIndex = fileName.IndexOf(dot);
            return (dotIndex == fileName.Length - Global.PdfExt.Length) &&
                    Helper.UnicodeToAscii(fileName) == fileName;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
        public static string ChangeAutosaveOptions(string ext, PdfCreatorFormat format)
        {
            string fileName = Helper.GetTemporaryFile(ext);
            FileInfo fileInfo = new FileInfo(fileName);
            autoSaveDirectory = fileInfo.DirectoryName;
            autoSaveFileName = Path.GetFileNameWithoutExtension(fileName);

            string formatStr = ((int)format).ToString();
            autoSaveFormat = formatStr;
            WriteOptions(false);

            return fileName;
        }

        public static void ChangeOptions()
        {
            //first, kill pdf creator process so that the option change take effect
            Process pdfCreatorProcess;
            bool pdfCreatorExited;
            Process[] processes = Process.GetProcessesByName(Global.PdfCreatorName);
            if (processes.Length > 0)
            {
                pdfCreatorProcess = processes[0];
                pdfCreatorProcess.Kill();
                pdfCreatorExited = pdfCreatorProcess.WaitForExit(Global.OperationTimeOut);
                if (!pdfCreatorExited)
                {
                    throw new Exception("Error: PDF Creator hangs");
                }
            }

            WriteOptions(true);
        }

        public static void ResetOptions()
        {
            RegistryKey hkcu = Registry.CurrentUser;
            hkcu = hkcu.OpenSubKey(@"Software\PDFCreator\Program", true);
            hkcu.SetValue("AutosaveDirectory", @"<MyFiles>\");
            hkcu.SetValue("AutosaveFileName", "<DateTime>");
            hkcu.SetValue("UseAutosave", "0");
            hkcu.SetValue("UseAutosaveDirectory", "0");
            hkcu.SetValue("AutosaveFormat", "0");
            hkcu.SetValue("AutosaveStartStandardProgram", "0");
            hkcu.SetValue("RunProgramAfterSaving", "0");
            hkcu.SetValue("RunProgramAfterSavingProgramme", "");
            hkcu.SetValue("RunProgramAfterSavingProgramname", "");
            hkcu.SetValue("RunProgramBeforeSaving", "0");
            hkcu.SetValue("RunProgramBeforeSavingProgramme", "");
            hkcu.SetValue("RunProgramBeforeSavingProgramname", "");
            hkcu.Close();
        }

        private static void WriteOptions(bool enableAutosave)
        {
            RegistryKey hkcu = Registry.CurrentUser;
            hkcu = hkcu.OpenSubKey(PdfCreatorProgramRegistry, true);

            if (enableAutosave)
            {
                //set auto save releated stuff
                hkcu.SetValue("UseAutosave", useAutoSave);
                hkcu.SetValue("UseAutosaveDirectory", useAutoSaveDirectory);
                hkcu.SetValue("AutosaveFormat", autoSaveFormat);
                hkcu.SetValue("AutosaveStartStandardProgram", autoSaveStartStandardProgram);
                hkcu.SetValue("RunProgramAfterSaving", runProgramAfterSaving);
                hkcu.SetValue("RunProgramAfterSavingProgramme", runProgramAfterSavingProgramme);
                hkcu.SetValue("RunProgramAfterSavingProgramname", runProgramAfterSavingProgramName);
                hkcu.SetValue("RunProgramBeforeSaving", runProgramBeforeSaving);
                hkcu.SetValue("RunProgramBeforeSavingProgramme", runProgramBeforeSavingProgramme);
                hkcu.SetValue("RunProgramBeforeSavingProgramname", runProgramBeforeSavingProgramName);
            }

            //set auto save directory and fileName
            hkcu.SetValue("AutosaveFormat", autoSaveFormat);
            hkcu.SetValue("AutosaveDirectory", autoSaveDirectory);
            hkcu.SetValue("AutosaveFileName", autoSaveFileName);
            hkcu.Close();

            hkcu = Registry.CurrentUser;
            hkcu.OpenSubKey(@"Software\PDFCreator\Printing", true);
            hkcu.SetValue("OnePagePerFile", "0");
            hkcu.Close();
        }

        public static string FormatToExtension(PdfCreatorFormat format)
        {
            return formatExtensionDict[format];
        }
    }
}

